package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //启动
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //全查
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        //返回
        return new PageResult(page.getTotal(),page.getResult());

    }

    /**
     * 根据DTO的categoryId查询所有套餐
     * @param setmealDTO
     * @return
     */
    @Override
    public List<SetmealVO> list(SetmealDTO setmealDTO) {
        List<SetmealVO> list = setmealMapper.getByCategoryId(setmealDTO);
        return list;
    }

    /**
     * 根据套餐id查询这个套餐里面的菜
     * @param setmealId
     * @return
     */
    @Override
    public List<DishItemVO> getDishesBySetmealId(Long setmealId) {
        return setmealMapper.getDishesBySetmealId(setmealId);
    }

    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal meal = setmealMapper.getById(id);
        BeanUtils.copyProperties(meal,setmealVO);
        return setmealVO;
    }

    @Override
    public void changeDishStatus(Integer status, Integer id) {
        if (status == 0) {
            setmealMapper.disenableDish(id);
        }
        else if (status == 1) {
            setmealMapper.enableDish(id);
        }
    }

}
